//
//  YMProgressView.m
//  QuPaiYunDemo
//
//  Created by 占益民 on 2018/3/29.
//  Copyright © 2018年 占益民. All rights reserved.
//

#import "YMProgressView.h"

#define RGBToColor(r,g,b)  [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0 alpha:1.0f];

@implementation YMProgressView{
    NSTimer * _timer;
    NSInteger _times;
    CGFloat _progress;
    CGFloat _lineWidth;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = RGBToColor(239, 75, 129);
        [self defaultParam];
    }
    return self;
}

- (void)defaultParam {
    _pointArray = [NSMutableArray arrayWithCapacity:0];
    _lineWidth = CGRectGetHeight(self.bounds) * [UIScreen mainScreen].scale;
    _colorNotice = [UIColor whiteColor];
    _colorProgress = RGBToColor(35, 42, 66);
    _colorSepatorPoint = [UIColor whiteColor];
    _colorSelect = [UIColor redColor];
    _selectedIndex = -1;
}

- (void)setShowBlink:(BOOL)showBlink{
    _showBlink = showBlink;
    [self destroyTimer];
    if (_showBlink) {
        [self startTimer];
    }
}


-(void)setShowNoticePoint:(BOOL)showNoticePoint{
    _showNoticePoint = showNoticePoint;
}

-(void)setVideoCount:(NSInteger)videoCount{
    if (_videoCount < videoCount) {
        [_pointArray addObject:@(_progress)];
    } else {
        [_pointArray removeAllObjects];
    }
    _videoCount = videoCount < 0 ? 0 :videoCount;
    _selectedIndex = -1;
}

-(void)updateProgress:(CGFloat)progress{
    _progress = progress;
    [self setNeedsDisplay];
}


- (CGFloat)endPointX{
    return _progress / _maxDuration * CGRectGetWidth(self.bounds);
}

- (BOOL)shouldShowNotice{
    return _progress < _minDuration;
}

- (void)reset{
    _videoCount = 0;
    [_pointArray removeAllObjects];
    [self updateProgress:0];
}


#pragma mark - timer

- (void)destroyTimer{
    [_timer invalidate];
    _timer = nil;
}


- (void)startTimer{
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];
    [_timer fire];
}


























@end
